﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using CE.User;
using CE.Global;
using CE.Report;

public partial class management_proposal_printpreview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("ManagementSecureLogin.aspx");
        }
        if (Session[CE.Global.General.InsParam] == null)
        {
            Response.Redirect("~/proposal_report.aspx");
        }
        if (!IsPostBack)
        {
            //txtHidTotalAmount.Text = "";
            if (Session[CE.Global.General.InsParam] != null)
            {
                string[] _params = Session[CE.Global.General.InsParam].ToString().Split(',');
                txtProvider.Text = _params[0].ToString();
                txtInsuranceType.Text = _params[1].ToString();
                txtReportType.Text = _params[2].ToString();
                txtReportDate1.Text = _params[3].ToString();
                txtReportDate2.Text = _params[4].ToString();
                txtCEID.Text = _params[5].ToString();
                txtAppStatus.Text = _params[6].ToString();
                txtAppType.Text = _params[7].ToString();
                txtUserTypeId.Text = _params[8].ToString();
                
                if (_params[1].ToString() == "CI")
                {
                    lblFormTitle.Text = "Car Insurance ";
                }
                else if (_params[1].ToString() == "HL")
                {
                    lblFormTitle.Text = "Home Loan ";
                }
                else if (_params[1].ToString() == "PA")
                {
                    lblFormTitle.Text = "Personal Accident ";
                }
                else if (_params[1].ToString() == "TA")
                {
                    lblFormTitle.Text = "Travel Insurance ";
                }
                else
                {
                    lblFormTitle.Text = "";
                }
            }


            string _strDate1 = txtReportDate1.Text;
            string _strDate2 = txtReportDate2.Text;
            if (txtReportType.Text.Trim() == "0")
            {
                lblDate.Text = GetMonth(_strDate1.Substring(4, 2).ToString());
                lblDate.Text += " " + _strDate1.Substring(0, 4).ToString();
            }
            else if (txtReportType.Text.Trim() == "1")
            {
                lblDate.Text = _strDate1.Substring(6, 2).ToString();
                lblDate.Text += " " + GetMonth(_strDate1.Substring(4, 2).ToString());
                lblDate.Text += " " + _strDate1.Substring(0, 4).ToString();
            }
            else if (txtReportType.Text.Trim() == "2")
            {
                lblDate.Text = _strDate1.Substring(6, 2).ToString();
                lblDate.Text += " " + GetMonth(_strDate1.Substring(4, 2).ToString());
                lblDate.Text += " " + _strDate1.Substring(0, 4).ToString();
                lblDate.Text += " to " + _strDate2.Substring(6, 2).ToString();
                lblDate.Text += " " + GetMonth(_strDate2.Substring(4, 2).ToString());
                lblDate.Text += " " + _strDate2.Substring(0, 4).ToString();
            }
            DateTime _printDate = CE.Global.General.GetUTCDateTime();
            lblPrintDate.Text = _printDate.Day.ToString("00") + " " + GetMonth(_printDate.Month.ToString("00")) + " " + _printDate.Year.ToString("0000") + " " + CE.Global.General.GetUTCDateTime().ToShortTimeString();
            //ShowCXCompanyInformation();

            //New Format Start
            if (txtReportType.Text.Trim() == "0")
            {
                lblReportType.Text = "Monthly";
            }
            else if (txtReportType.Text.Trim() == "1")
            {
                lblReportType.Text = "Daily";
            }
            else if (txtReportType.Text.Trim() == "2")
            {
                lblReportType.Text = "Customize";
            }
            if (txtInsuranceType.Text.Trim() == "CI")
            {
                lblReportType.Text += " - Car Insurance";
            }
            else if (txtInsuranceType.Text.Trim() == "PA")
            {
                lblReportType.Text += " - Personal Accident";
            }
            else if (txtInsuranceType.Text.Trim() == "TI")
            {
                lblReportType.Text += " - Travel Insurance";

            }
            lblStatementDate.Text = lblDate.Text;
            //New Format End

            if (txtProvider.Text.Trim() != "0")
            {
                ShowProviderCompanyInformation(txtProvider.Text.Trim());
            }
            sqlDsProposal.DataBind();
            if (txtProvider.Text.Trim() == "0")
            {
                divRecordDetails.Visible = true;
                divProviderAddress.Visible = false;

                grvProposalSummary.DataBind();
                grvProposalSummary.Visible = true;
                grvIndProposalSummary.Visible = false;
                lblTotalAmount.Visible = true;
            }
            else
            {
                divRecordDetails.Visible = false;
                divProviderAddress.Visible = true;

                grvIndProposalSummary.DataBind();
                grvProposalSummary.Visible = false;
                grvIndProposalSummary.Visible = true;
                lblTotalAmount.Visible = true;                
            }
            lblTotalCases.Text = grvProposalSummary.Rows.Count.ToString();
            lblTotalRecord.Text = lblTotalCases.Text + " record(s).";
        }
    }

    protected void ShowCXCompanyInformation()
    {
        CE.Company.CompanyInformation _company = new CE.Company.CompanyInformation();
        CE.Company.CompanyInformationDB _companydb = new CE.Company.CompanyInformationDB();
        string _strCompanyInformation = "";
        _company = _companydb.GetCompanyInformation(1);
        if (_company != null)
        {

            _strCompanyInformation += "<strong>" + _company.CompanyName + "</strong>";
            _strCompanyInformation += "<br />" + _company.Address1.Trim();
            _strCompanyInformation += ",&nbsp;" + _company.Address2.Trim();
            _strCompanyInformation += ",&nbsp;" + _company.Country.Trim();
            _strCompanyInformation += " - " + _company.PostCode.Trim();
            _strCompanyInformation += "<br />Tel : " + _company.Telephone;
            _strCompanyInformation += ",&nbsp;Email : " + "support@gluaygluay.com";// _company.EmailAddress;
        }
        lblCompanyInformation.Text = _strCompanyInformation;
    }

    protected void ShowProviderCompanyInformation(string ProviderId)
    {
        CE.Company.Company _company = new CE.Company.Company();
        CE.Company.CompanyDB _companydb = new CE.Company.CompanyDB();
        string _strCompanyInformation = "";
        Int64 _providercompanyId = Convert.ToInt64(ProviderId.ToString());
        _company = _companydb.GetCompany(_providercompanyId);
        if (_company != null)
        {
            lblInsurer.Text = _company.CompanyNameDescription; 
            _strCompanyInformation += "<strong>" + _company.CompanyNameDescription + "</strong>"; // _company.UnitNo1.Trim() + " - " + _company.UnitNo2.Trim() + ", " 
            _strCompanyInformation += "<br />" + _company.Street.Trim() + " " + _company.City.Trim() + " " + _company.PostCode;
            //_strCompanyInformation += "<br />" + _company.Street;
            ////if(_company.City != string.Empty)
            ////    _strCompanyInformation += "<br />" + _company.City;
            //_strCompanyInformation += "<br />" + _company.City;
            //_strCompanyInformation += " " + _company.PostCode;
            //_strCompanyInformation += "<br />Tel : " + _company.PhoneNo1;
            //_strCompanyInformation += "<br />Email :" + _company.Email1;
        }
        lblProviderCompanyInformation.Text = _strCompanyInformation;
        //lblInsurer.Text = _company.CompanyNameDescription; 
    }

    public string GetMonth(string month)
    {
        string result = "";
        if (month == "01")
        {
            result = " Jan ";
        }
        else if (month == "02")
        {
            result = " Feb ";
        }
        else if (month == "03")
        {
            result = " Mar ";
        }
        else if (month == "04")
        {
            result = " Apr ";
        }
        else if (month == "05")
        {
            result = " May ";
        }
        else if (month == "06")
        {
            result = " Jun ";
        }
        else if (month == "07")
        {
            result = " Jul ";
        }
        else if (month == "08")
        {
            result = " Aug ";
        }
        else if (month == "09")
        {
            result = " Sep ";
        }
        else if (month == "10")
        {
            result = " Oct ";
        }
        else if (month == "11")
        {
            result = " Nov ";
        }
        else if (month == "12")
        {
            result = " Dec ";
        }
        return result;
    }

    protected void grvProposalSummary_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (txtProvider.Text.Trim() == "0")
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[2].Attributes.CssStyle.Add("text-align", "left");
                e.Row.Cells[3].Attributes.CssStyle.Add("text-align", "left");
                e.Row.Cells[4].Attributes.CssStyle.Add("text-align", "left");
                e.Row.Cells[5].Attributes.CssStyle.Add("text-align", "left");
                e.Row.Cells[6].Attributes.CssStyle.Add("text-align", "right");
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[2].Attributes.CssStyle.Add("text-align", "left");
                e.Row.Cells[3].Attributes.CssStyle.Add("text-align", "left");
                e.Row.Cells[4].Attributes.CssStyle.Add("text-align", "left");
                e.Row.Cells[5].Attributes.CssStyle.Add("text-align", "left");
                e.Row.Cells[6].Attributes.CssStyle.Add("text-align", "right");

                float _premium = Convert.ToSingle(DataBinder.Eval(e.Row.DataItem, "AnnualPremium"));
                float _toppremium = Convert.ToSingle(txtHidTotalAmount.Text);
                _toppremium = _toppremium + _premium;
                txtHidTotalAmount.Text = _toppremium.ToString();
                lblTotalAmount.Text = "Total Premium : " + _toppremium.ToString("฿#,000.00");
                //lblTotalPremium.Text = _toppremium.ToString("$#,000.00");
            }
        }
    }

    protected void grvIndProposalSummary_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (txtProvider.Text.Trim() != "0")
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[2].Attributes.CssStyle.Add("text-align", "left");
                e.Row.Cells[3].Attributes.CssStyle.Add("text-align", "left");
                e.Row.Cells[4].Attributes.CssStyle.Add("text-align", "left");
                e.Row.Cells[5].Attributes.CssStyle.Add("text-align", "right");
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[2].Attributes.CssStyle.Add("text-align", "left");
                e.Row.Cells[3].Attributes.CssStyle.Add("text-align", "left");
                e.Row.Cells[4].Attributes.CssStyle.Add("text-align", "left");
                e.Row.Cells[5].Attributes.CssStyle.Add("text-align", "right");

                float _premium = Convert.ToSingle(DataBinder.Eval(e.Row.DataItem, "AnnualPremium"));
                float _toppremium = Convert.ToSingle(txtHidTotalAmount.Text);
                _toppremium = _toppremium + _premium;
                txtHidTotalAmount.Text = _toppremium.ToString();
                lblTotalAmount.Text = "Total Premium : " + _toppremium.ToString("฿#,000.00");
                //lblTotalPremium.Text = _toppremium.ToString("$#,000.00");
            }
        }
    }

    public string GetReferenceNumber(string Number, string ProviderId, string InsType)
    {
        string convertno = CE.Global.General.GetRefNumber(Convert.ToInt64(Number.ToString()));
        string _RefNo = "";
        if (ProviderId == "11")
        {
            _RefNo = "AZC" + convertno.ToString();
        }
        else if (ProviderId == "12")
        {
            _RefNo = "MSI" + convertno.ToString();
        }
        else if (ProviderId == "16")
        {
            _RefNo = "AXA" + convertno.ToString();
        }
        else if (ProviderId == "29")
        {
            _RefNo = "LMG" + convertno.ToString();
        }
        else if (ProviderId == "31")
        {
            _RefNo = "CTS" + convertno.ToString();
        }
        else if (ProviderId == "32")
        {
            _RefNo = "BKI" + convertno.ToString();
        }
        else if (ProviderId == "33")
        {
            _RefNo = "MTI" + convertno.ToString();
        }
        else if (ProviderId == "35")
        {
            _RefNo = "KPI" + convertno.ToString();
        }
        else if (ProviderId == "39")
        {
            _RefNo = "MIR" + convertno.ToString();
        }
        else if (ProviderId == "41")
        {
            _RefNo = "KSK" + convertno.ToString();
        }
        else if (ProviderId == "43")
        {
            _RefNo = "SMK" + convertno.ToString();
        }
        else if (ProviderId == "45")
        {
            _RefNo = "VRY" + convertno.ToString();
        }
        else if (ProviderId == "46")
        {
            _RefNo = "TMS" + convertno.ToString();
        }
        else if (ProviderId == "50")
        {
            _RefNo = "SCS" + convertno.ToString();
        }
        else if (ProviderId == "58")
        {
            _RefNo = "TVV" + convertno.ToString();
        }
        return _RefNo;
    }

}
